package com.android.locationapi.maps;

import java.io.InputStream;
import java.util.ArrayList;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;

import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.os.IBinder;
import android.util.Log;




public class NewService extends Service {
		 
	 private LocationManager lm;

	Location location ;

	public void onCreate() {
		super.onCreate();

		Log.v("NEW SERVICE", "In service");
		  lm = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
		  lm.requestLocationUpdates(LocationManager.NETWORK_PROVIDER,0 * 1000, 0, new GeoUpdateHandler() );
		   Location lastKnownLoc = lm.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
		  
		   Location lastLocation = lm.getLastKnownLocation( LocationManager.NETWORK_PROVIDER );
		   int longTemp;
		   int latTemp;
		    if ( lastLocation != null )
		    {
		   
try{
		   
		      longTemp = (int)(lastKnownLoc.getLongitude()* 1000000);
		      latTemp = (int)(lastKnownLoc.getLatitude() * 1000000);
		     Log.v("LOCATION SERVICE", "Sent " + longTemp);
       	  Log.v("LOCATION SERVICE", "Sent " + latTemp);
       
}
catch (NullPointerException e)
{
	
}
		    }

		    longTemp = 44410000;
		    latTemp = -788960000;
	}


	public class GeoUpdateHandler implements LocationListener {
		
		@Override
		public void onLocationChanged(Location location) {
			double lat =  location.getLatitude();
			double lng =  location.getLongitude();

						
	          try{
	        	  ArrayList<NameValuePair> data = new ArrayList<NameValuePair>();
	        	  data.add(new BasicNameValuePair("username", serv.sessionusername));
	        	  data.add(new BasicNameValuePair("latitude", Double.toString(lat)));
	        	  data.add(new BasicNameValuePair("longitude", Double.toString(lng)));
	        	  Log.v("Location Service", "Sent " + serv.sessionusername);
	        	  Log.v("Location Service", "Sent " + lat);
	        	  Log.v("Location Service", "Sent " + lng);
	        	  HttpClient httpclient = new DefaultHttpClient();
	              HttpPost httppost = new HttpPost(serveripaddress.servipaddr+"location.php");
	              httppost.setEntity(new UrlEncodedFormEntity(data));
	              Log.v("Server - Location Update", "Here now");
	              HttpResponse response = httpclient.execute(httppost);
	              Log.v("Server", "Waiting for response!!!");
                  HttpEntity entity = response.getEntity();
                  Log.v("Server", "In here now");
                  InputStream is = entity.getContent();
                  Log.v("SERVER: Returned from the server 0 : ", is.toString());
	          }
		    catch(Exception e){
           Log.v("log_tag", "Error in http connection "+e.toString());
        }
		}

		@Override
		public void onProviderDisabled(String provider) {
		}

		@Override
		public void onProviderEnabled(String provider) {
		}

		@Override
		public void onStatusChanged(String provider, int status, Bundle extras) {
		}
		

	}
	@Override
	public IBinder onBind(Intent intent) {
		// TODO Auto-generated method stub
		return null;
	}
}